<!--
  module - 用户与权限--- 角色

  @author wuh 2020-05-13
-->
<template>
  <div>
    <div class="search-bar">
      <div class="base-condition">
        <el-row justify="space-between">
          <el-col :span="12">
            <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal">
              <el-menu-item index="1">全部路由</el-menu-item>
            </el-menu>
          </el-col>
          <el-col :span="12" style="text-align:right;">
            <el-input
              v-model="listQuery.titleOrRouter"
              placeholder="请输入路由标题/地址"
              class="radius-input"
              style="width: 260px;margin-top:7px;"
              clearable
              @keyup.enter.native="search"
            >
              <i slot="prefix" class="el-input__icon el-icon-search" @click="search" />
            </el-input>
          </el-col>
        </el-row>
      </div>
    </div>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>路由列表<em>（共{{ total }}条记录）</em></span>
      </div>
      <div>
        <el-row :gutter="20">
          <el-col :span="leftWide">
            <!-- start 列表 -->
            <el-table
              v-loading="listLoading"
              :data="list"
              style="width: 100%"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
              row-key="routerId"
              :fit="true"
              :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            >
              <el-table-column label="路由标题" prop="title" min-width="20%" :show-overflow-tooltip="true" />
              <el-table-column label="页面路由地址" prop="path" min-width="20%" :show-overflow-tooltip="true" />
              <el-table-column label="权限码" prop="routerKey" min-width="20%" :show-overflow-tooltip="true" />
              <!-- <el-table-column label="是否隐藏" :width="100" :show-overflow-tooltip="true" align="center">
                <template slot-scope="scope">
                  <el-button v-if="scope.row.isHide" type="success" icon="el-icon-check" size="mini" circle />
                  <el-button v-else type="info" icon="el-icon-check" size="mini" circle />
                </template>
              </el-table-column> -->
              <el-table-column label="资源类型" min-width="10%" :show-overflow-tooltip="true" align="center">
                <template slot-scope="scope">
                  <span v-if="scope.row.type=='MENU'">菜单</span>
                  <span v-else-if="scope.row.type=='PAGE'">页面</span>
                  <span v-else-if="scope.row.type=='FUNC'">功能项</span>
                  <span v-else>--</span>
                </template>
              </el-table-column>

              <el-table-column label="操作" class-name="small-padding fixed-width" min-width="20%" align="center">
                <template slot-scope="scope">
                  <template>
                    <el-link v-if="scope.row.path" type="primary" size="mini" @click="handleMenuManage(scope.row)">
                      接口权限
                    </el-link>
                  </template>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
          <el-col :span="rightWide">
            <el-card class="box-card" style="margin: 0px;box-shadow: 0px 0px 0px #000;height: 550px;">
              <div slot="header" class="clearfix">
                <span>[{{ title }}]的菜单权限</span>
                <el-link
                  v-waves
                  size="mini"
                  type="primary"
                  class="filter-item"
                  style="float: right; margin-left: 10px;"
                  @click="handleSaveMenu"
                >
                  保存
                </el-link>
              </div>
              <div class="text item _resourceRoutes_scroll_box" style="padding-bottom: 20px; height:430px;">
                <el-input v-model="filterText" placeholder="输入关键字进行过滤" />
                <el-scrollbar style="height:100%; margin-top:5px;">
                  <el-tree
                    ref="tree"
                    v-loading="loadingMenu"
                    :data="sysRightRoutersData"
                    :props="props"
                    show-checkbox
                    node-key="url"
                    check-strictly
                    class="sysRole-scrollbar"
                    :default-expand-all="true"
                    :filter-node-method="filterNode"
                  >
                    <span slot-scope="{ node }" style="flex: 1; display: flex; align-items: center; justify-content: space-between; font-size: 14px; padding-right: 8px;">
                      <span>{{ node.label }}</span>
                      <span style="font-size: 12px; color: #ccc;">{{ node.key }}</span>
                    </span>
                  </el-tree>
                </el-scrollbar>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </el-card>

  </div>
</template>

<script>
// api
import { queryRightsRoutersList, queryAllApi, queryApiPathListByRouterId, updateSysRightsRoutersApi } from '@/api/userAndRole/sysRightsRouters.js'
// 水波纹插件
import waves from '@/directive/waves'

export default {
  name: 'SysRouters',
  directives: { waves },
  data() {
    return {
      activeIndex: '1',
      total: 0,
      list: [],
      listLoading: false,
      leftWide: 24,
      rightWide: 0,
      title: '',
      listQuery: {
        titleOrRouter: ''
      },
      count: 1,
      routerId: '',
      loadingMenu: true,
      props: {
        label: 'title'
      },
      menuFlag: true,
      sysRightRoutersData: [],
      filterText: ''
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.queryRightsRoutersList()
  },
  methods: {
    queryRightsRoutersList() {
      const curVue = this
      curVue.listLoading = true
      const query = {
        titleOrRouter: curVue.listQuery.titleOrRouter
      }
      queryRightsRoutersList(query).then(res => {
        if (res.code === 1) {
          curVue.listLoading = false
          curVue.list = res.data.routersList
          curVue.total = res.data.totalCount
        } else {
          curVue.noticeUtil.errorMsg('路由列表获取失败！')
        }
      })
    },
    // handHide(data) {
    //   const curVue = this
    //   const formObj = {
    //     routerId: data.routerId,
    //     isHide: !data.isHide
    //   }
    //   let mes = ''
    //   if (data.isHide) {
    //     mes = '您确定要显示此路由菜单吗?'
    //   } else {
    //     mes = '您确定要隐藏此路由菜单吗?'
    //   }
    //   this.noticeUtil.confirm(mes).then(() => {
    //     updateRightsRouters(formObj).then(res => {
    //       if (res.code === 1) {
    //         curVue.noticeUtil.successMsg('操作成功！')
    //         curVue.queryRightsRoutersList()
    //       }
    //     })
    //   }).catch(() => {
    //     curVue.noticeUtil.warningMsg('已取消操作！')
    //   })
    // },
    search() {
      this.queryRightsRoutersList()
    },
    handleMenuManage(rows) {
      const that = this
      if (rows.routerId !== this.routerId) {
        if (this.routerId) {
          that.sysRightRoutersData = []
          this.loadingMenu = true
        }
        this.menuFlag = false
        this.routerId = rows.routerId
        this.title = rows.title
        this.leftWide = 15
        this.rightWide = 9
        const param = {
          routerId: rows.routerId
        }
        // 查询当前角色下的资源
        queryAllApi().then(res => {
          that.loadingMenu = false
          that.sysRightRoutersData = res.data
          queryApiPathListByRouterId(param).then(res => {
            res.data.forEach((i, n) => {
              var node = that.$refs.tree.getNode(i)
              // if (node.isLeaf) {
              that.$refs.tree.setChecked(node, true)
              // }
            })
          })
        })
      } else {
        this.loadingMenu = true
        this.menuFlag = true
        this.routerId = ''
        this.title = ''
        this.leftWide = 24
        this.rightWide = 0
      }
    },
    handleSaveMenu() {
      const curVue = this
      const checkedKeys = this.$refs.tree.getCheckedKeys()
      const selfKeys = this.$refs.tree.getHalfCheckedKeys()
      const allKeys = checkedKeys.concat(selfKeys)
      const param = {
        routerId: this.routerId,
        apiPaths: allKeys
      }
      updateSysRightsRoutersApi(param).then(res => {
        if (res.code === 1) {
          curVue.noticeUtil.successMsg(res.mesg)
          curVue.loadingMenu = true
          curVue.menuFlag = true
          curVue.routerId = ''
          curVue.title = ''
          curVue.leftWide = 24
          curVue.rightWide = 0
        } else {
          curVue.noticeUtil.errorMsg(res.mesg)
        }
      })
    },
    filterNode(value, data) {
      if (!value) {
        return true
      }

      return (data.title + data.url).indexOf(value) !== -1
    }
  }
}
</script>

<style lang="scss" scoped>
.search-bar{
  position: relative;
  z-index: 2000;
  width: 100%;
  .base-condition{
    padding: 0 40px;
    background: #fff;
    box-shadow: 0 2px 4px 0 rgba(0,0,0,.14);
    .el-menu-demo,.el-menu-demo *{
      line-height: 50px;
      height: 50px;

      .el-menu-item{
        padding: 0 8px;
        margin-right: 20px;
      }
    }
  }
  .more-condition{
    position: absolute;
    top: 65px;
    background: #fff;
    width: 100%;
    height: auto;
    padding: 20px 40px;
    box-shadow: 0 2px 4px 0 rgba(0,0,0,.14);
  }
  .query-result{
    margin-top: 5px;
    font-size: 12px;
    color: #4c596e;
    padding: 15px 30px;
    background: #fbfdff;
  }
  .query-result *{
    line-height: 25px;
  }
  .filter-item{
    display: inline-block;
    padding: 0 10px;
    margin-left: 10px;
    border-radius: 12px;
    background: #e5e9f2;
    color: #7f8997;
  }
  .filter-item i{
    margin-left: 5px;
    font-weight: 700;
    cursor: pointer;
  }

}
.radius-input >>> input{
  border-radius: 100px !important;
}
</style>
<style>
._resourceRoutes_scroll_box .el-scrollbar__wrap{
  overflow-x: hidden;
}
</style>
